package com.tangruojie.controller;

import com.tangruojie.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    /**
     * 普通参数：形参名称必须和请求参数名称一致
     *
     * @return
     */
    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name, int age) {
        System.out.println("common param:" + name);
        System.out.println("common param:" + age);
        return "{'module':'common param'}";
    }

    /**
     * 普通参数：参数名称和形参名称不一致
     *
     * @return
     */
    @RequestMapping("/commonParamDifferentName")
    @ResponseBody
    public String commonParamDifferentName(@RequestParam("name") String userName, int age) {
        System.out.println("common param different name:" + userName);
        System.out.println("common param different name:" + age);
        return "{'module':'common param different name'}";
    }

    /**
     * POJO参数
     *
     * @param user
     * @return
     */
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user) {
        System.out.println("pojo param:" + user);
        return "{'module':'pojo param'}";
    }

    /**
     * 嵌套POJO参数
     *
     * @param user
     * @return
     */
    @RequestMapping("/pojoContainPojoParam")
    @ResponseBody
    public String pojoContainPojoParam(User user) {
        System.out.println("pojo contain pojo param:" + user);
        return "{'module':'pojo contain pojo param'}";
    }

    /**
     * 数组参数
     *
     * @param likes
     * @return
     */
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes) {
        System.out.println("array param:" + Arrays.toString(likes));
        return "{'module':'array param'}";
    }

    /**
     * 集合参数
     * 加上 @RequestParam 注解后，SpringMVC 才知道要从请求的 Query 参数中取值绑定到方法参数中，否则就不会自动绑定！
     *
     * @param likes
     * @return
     */
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes) {
        System.out.println("list param:" + likes);
        return "{'module':'list param'}";
    }

    /**
     * 集合参数：json格式
     *
     * @param likes
     * @return
     */
    @RequestMapping("/listParamForJson")
    @ResponseBody
    public String listParamForJson(@RequestBody List<String> likes) {
        System.out.println("list param for json:" + likes);
        return "{'module':'list param for json'}";
    }

    /**
     * POJO参数：json格式
     *
     * @param user
     * @return
     */
    @RequestMapping("/pojoParamForJson")
    @ResponseBody
    public String pojoParamForJson(@RequestBody User user) {
        System.out.println("pojo param for json:" + user);
        return "{'module':'pojo param for json'}";
    }

    /**
     * 集合POJO参数：json格式
     *
     * @param list
     * @return
     */
    @RequestMapping("/liatPojoParamForJson")
    @ResponseBody
    public String liatPojoParamForJson(@RequestBody List<User> list) {
        System.out.println("list pojo param for json:" + list);
        return "{'module':'list pojo param for json'}";
    }

    /**
     * 日期参数
     *
     * @param date
     * @return
     */
    @RequestMapping("/dataParam")
    @ResponseBody
    public String dataParam(
            Date date,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date2,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date3
    ) {
        System.out.println("data param:" + date);
        System.out.println("data param:" + date2);
        System.out.println("data param:" + date3);
        return "{'module':'data param'}";
    }

}











